//
//  FindPwdController.m
//  QQplayground
//
//  Created by zhangxy on 2018/5/23.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "FindPwdController.h"
//
//  LoginController.m
//  QQplayground
//
//  Created by zhangxy on 2018/5/21.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "LoginController.h"
#import "BaseTabBarController.h"
#import <UMShare/UMShare.h>
#import "ZCTTMLEmojiLabel.h"
#import "ZTextField.h"
#import "BaseTabBarController.h"
#import "RegisterController.h"
#import "SVWebViewController.h"
#import "SearchCodeController.h"
#import "FindPwdController.h"

@interface FindPwdController ()<UITextFieldDelegate,UIGestureRecognizerDelegate>{
    CGFloat startY;
    int isPhoneLogin;
}

@property(nonatomic,strong) UIView *loginView;
@property(nonatomic,strong) UILabel *labTips;
@property(nonatomic,strong) ZTextField *txtName;
@property(nonatomic,strong) ZTextField *txtPwd;
@property(nonatomic,strong) UIButton *btnTempLogin;
@property(nonatomic,strong) UIButton *comBtn;
@property(nonatomic,strong) UIButton *btnGetCode;



@end

@implementation FindPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    self.view.userInteractionEnabled = YES;
    [self createVCView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyboard:)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //    self.navigationController.navigationBar.hidden = NO;
}

-(void)backAction{
    DLog(@"%@",self.view.window.rootViewController);
    [super backAction];
}

// 隐藏键盘
-(void)hiddenKeyboard:(id)sender
{
    [_txtPwd resignFirstResponder];
    [_txtName resignFirstResponder];
}


-(void)doThirdLogin:(UIButton *) sender{
    if(sender.tag == 10){
        DLog(@"普通登录");
        if (convertToString(_txtName.text).length == 0){
            [PopUtil showMessage:@"宝宝你的手机号呢"];
            return;
        } else if(convertToString(_txtName.text).length > 0 && convertToString(_txtName.text).length < 8){
            [PopUtil showMessage:@"手机号码输入错啦"];
            return;
        }
        if(convertToString(_txtPwd.text).length == 0){
            [PopUtil showMessage:@"不接受没有验证码哦"];
            return;
        } else {
            [self checkCode];
            return ;
        }
        RegisterController *vc = [[RegisterController alloc] init];
        vc.phone_code = _txtPwd.text;
        vc.phone = _txtName.text;
        vc.phone_area_code = convertToString(_btnTempLogin.titleLabel.text);
        vc.isRegister = NO;
        [self openPushVC:vc];
        
    }
    if(sender.tag == 11){
        DLog(@"获取验证码");
        if(convertToString(_txtName.text).length < 8){
            [self.view makeToast:@"手机号码不正确！"];
            return;
        }
        
        [self imgCodeShow];
        
    }
}

-(void)checkCode{
    NSDictionary *p = @{@"phone":convertToString(_txtName.text),
                        @"phone_area_code":convertToString(_btnTempLogin.titleLabel.text),
                        @"sms_code":convertToString(_txtPwd.text),
                        @"password":convertToString(_txtPwd.text)
                        };
    [NetworkUtil POST:API_FindPwd parameters:p success:^(id responseObject, NSDictionary *dict) {
//        [PopUtil showMessage:dict[@"msg"]];
//        [self.navigationController popToRootViewControllerAnimated:YES];
        RegisterController *vc = [[RegisterController alloc] init];
        vc.phone_code = _txtPwd.text;
        vc.phone = _txtName.text;
        vc.phone_area_code = convertToString(_btnTempLogin.titleLabel.text);
        vc.isRegister = NO;
        [self openPushVC:vc];
    } failure:^(NSError *error, NSString *message) {
        NSLog(@"message++++++++++++++++%@",message);
        message = @"验证码错了呢，替你着急";
        [PopUtil showMessage:message];
    }];
}
-(void)searchCode:(UIButton *)sender{
    SearchCodeController *vc = [[SearchCodeController alloc] init];
    [vc setZZPageResultBlock:^(id obj) {
        [_btnTempLogin setTitle:convertToString(obj) forState:0];
    }];
    [self openPushVC:vc];
}

- (void)imgCodeShow
{
    if(convertToString(_txtName.text).length < 8){
        [PopUtil showMessage:@"请输入手机号码"];
        return;
    }
    //发送网络请求，想服务器端验证图形验证码的正确性
    NSDictionary *params = [[NSDictionary alloc] initWithObjects:@[_txtName.text,convertToString(_btnTempLogin.titleLabel.text)] forKeys:@[@"phone",@"phone_area_code"]];
    DLog(@"params：%@",params);
    [NetworkUtil POST:API_SendSmsPwd parameters:params success:^(id responseObject, NSDictionary *dict) {
        [PopUtil showMessage:@"验证码已发送" positionType:PopPositionTypeMiddle];
        [_btnGetCode setTitle:@"重新发送" forState:0];
    } failure:^(NSError *error, NSString *message) {
        NSLog(@"验证码message-------------%@", message);
        if(convertToString(message).length>0){
            [PopUtil showMessage:message positionType:PopPositionTypeMiddle];
        }
    }];
}



-(void)createVCView{
    UIImage *backImage =[UIImage imageNamed:@"base_bg.jpg"];
    self.view.layer.contents=(id)backImage.CGImage;
    //    self.view.layer.backgroundColor = [UIColor clearColor].CGColor;
    self.view.layer.backgroundColor = UIColorFromRGB(0x67ACA6).CGColor;
    
    
    
    UIButton *backBtn = [self createButton:@"btn_back_title_sel"];
    [backBtn setFrame:CGRectMake(10, StatusBarHeight, 44, 44)];
    [backBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [backBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *titleBtn = [self createButton];
    [titleBtn setFrame:CGRectMake(54, StatusBarHeight, ScreenWidth - 108, 44)];
    [titleBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [titleBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [titleBtn setTitle:@"找回密码" forState:0];
    
    
    
    [self createTopView];
    
    _loginView = [[UIView alloc] initWithFrame:CGRectMake(0, startY, ScreenWidth, ScreenHeight-startY)];
    _loginView.backgroundColor = UIColor.clearColor;
    
    
    _loginView.hidden = NO;
    
    _labTips.hidden = YES;
    
    [self.view addSubview:_loginView];
    
    
    [self createLoginView];
    
    
    [self.view addSubview:backBtn];
    [self.view addSubview:titleBtn];
}

-(void)createTopView{
    CGFloat th = 120;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, ScreenWidth, th)];
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/2 - 100, 0, 200, 30)];
    [iconView setImage:[UIImage imageNamed:@"qzone"]];
    [iconView setContentMode:UIViewContentModeScaleAspectFit];
    [iconView setBackgroundColor:UIColor.clearColor];
    
    
    [topView addSubview:iconView];
    
    [self.view addSubview:topView];
    startY = CGRectGetMaxY(topView.frame) + 28;
}

-(void)createLoginView{
    
    CGFloat y = 0;
    
    UIView *itemBgView = [[UIView alloc] initWithFrame:CGRectMake(20, y, ScreenWidth-40, 52)];
    itemBgView.layer.cornerRadius = 11.0f;
    itemBgView.layer.masksToBounds  = YES;
    [itemBgView setBackgroundColor:UIColorFromRGB(TextWhiteColor)];
    [_loginView addSubview:itemBgView];
    
    _txtName = [self createTextField:@"请输入手机号" y:y];
    [_txtName setFrame:CGRectMake(50, 8.5, ScreenWidth - 40 - 60, 35)];
    [_txtName setKeyboardType:UIKeyboardTypePhonePad];
    [_loginView addSubview:_txtName];
    
    
    _btnTempLogin = [[UIButton alloc] initWithFrame:CGRectMake(20, 8.5, 55, 35)];
    [_btnTempLogin.titleLabel setFont:FontSeventeen];
    [_btnTempLogin setTitleColor:UIColorFromRGB(BgTitleColor) forState:0];
    [_btnTempLogin setTitle:@"+86" forState:0];
    [_btnTempLogin addTarget:self action:@selector(searchCode:) forControlEvents:UIControlEventTouchUpInside];
    [_loginView addSubview:_btnTempLogin];
    
    
    y = y + 15 + 52;
    
    _txtPwd = [self createTextField:@"请输入验证码" y:y];
    [_loginView addSubview:_txtPwd];
    
    _btnGetCode = [self createButton:@""];
    [_btnGetCode setFrame:CGRectMake(ScreenWidth - 20 - 90, y, 80, 52)];
    [_btnGetCode setBackgroundColor:UIColor.whiteColor];
    _btnGetCode.layer.cornerRadius = 29.0f;
    _btnGetCode.layer.masksToBounds = YES;
    [_btnGetCode setTitle:@"获取验证码" forState:0];
    [_btnGetCode setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnGetCode setTitleColor:UIColorFromRGB(BgTitleColor) forState:0];
    [_btnGetCode.titleLabel setFont:FontFiftent];
    _btnGetCode.tag = 11;
    [_btnGetCode addTarget:self action:@selector(doThirdLogin:) forControlEvents:UIControlEventTouchUpInside];
    [_loginView addSubview:_btnGetCode];
    
    
    y = y + 15 + 52;
    
    
    UIButton *_comBtn = [self createButton:@""];
    [_comBtn setFrame:CGRectMake(ScreenWidth/2 - 110/2, y, 110, 52)];
    [_comBtn setBackgroundColor:UIColorFromRGB(TextWhiteColor)];
    _comBtn.layer.cornerRadius = 26.0f;
    _comBtn.layer.masksToBounds = YES;
    [_comBtn setTitle:@"下一步" forState:0];
    [_comBtn setTitleColor:UIColorFromRGB(TextSizeEightColor) forState:0];
    [_comBtn.titleLabel setFont:FontSeventeen];
    _comBtn.tag = 10;
    [_comBtn addTarget:self action:@selector(doThirdLogin:) forControlEvents:UIControlEventTouchUpInside];
    [_loginView addSubview:_comBtn];
    
    y = y + 30 + 52;
    
    _labTips = [[UILabel alloc] initWithFrame:CGRectMake(20, y, ScreenWidth - 20, 60)];
    [_labTips setFont:FontSeventeen];
    _labTips.numberOfLines = 0;
    [_labTips setText:@"如果您是使用邮箱注册的用户，请登录Q友乐园网站 (www.qzone.cc)使用电脑端提供的找回密码功能。"];
    [_labTips setTextAlignment:NSTextAlignmentLeft];
    [_labTips setTextColor:UIColorFromRGB(TextWhiteColor)];
    
    [_loginView addSubview:_labTips];
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if(convertToString(_txtName.text).length > 0 && convertToString(_txtPwd.text).length > 0){
        [_comBtn setTitleColor:UIColorFromRGB(BgTitleColor) forState:0];
        _comBtn.enabled = YES;
    }else{
        [_comBtn setTitleColor:UIColorFromRGB(TextSizeEightColor) forState:0];
        _comBtn.enabled = NO;
    }
}


-(UIButton *) createButton:(NSString *) imageName{
    UIButton *btn = ({
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:convertToString(imageName)] forState:0];
        backBtn;
    });
    return btn;
}

-(ZTextField *) createTextField:(NSString *) placeHolder y:(CGFloat )ty{
    ZTextField *btn = ({
        ZTextField *backBtn = [ZZViewUtils createZTextField:placeHolder padding:20];
        [backBtn setFrame:CGRectMake(20, ty, ScreenWidth-40, 52)];
        backBtn.delegate = self;
        backBtn.clearsContextBeforeDrawing = YES;
        backBtn;
    });
    return btn;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
